package aiball

import org.springframework.dao.DataIntegrityViolationException

class DeleteHistoryService {

    public deleteHistory(){
		Date d1 = new Date();
		def datum =new Date(d1.getYear(),d1.getMonth(),d1.getDate(),0,0,0);
		datum -= 5
		
		def historyToDelete = History.findAllByTime(datum)
		def gamechartsToDelete = Gamecharts.findAllByTime(datum)
		
		for(int i=0; i<gamechartsToDelete.size(); i++)
		{
			def playerstatsToDelete = PlayerStats.findAllByGame(gamechartsToDelete.get(i))
			for(int j=0; j<playerstatsToDelete.size(); j++)
			{
				def playerstat = PlayerStats.get(playerstatsToDelete.get(j).id)
				try{
					playerstat.delete()
				}
				catch (DataIntegrityViolationException e) {
				}	
			}
			
			def gamechart = Gamecharts.get(gamechartsToDelete.get(i).id)
			try{
				gamechart.delete()
				}
				catch (DataIntegrityViolationException e) {
				}
		}
		
		for(int i=0; i<historyToDelete.size(); i++)
		{
			def history = History.get(historyToDelete.get(i).id)
			try{
				history.delete()
				}
				catch (DataIntegrityViolationException e) {
				}
		}
	}
}
